/**
 *  *2010-6-28 上午02:16:54  add 
 */
package com.base.myproject.client.system;

import com.base.myproject.client.jdbc.DataSet;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.KeyNav;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.CenterLayout;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;

/**
 * 用户自主的操作，只用于查询，无需密码，用用户的卡号进行登录
 * 
 * @author Administrator 2010-6-28 javazgw@gmail.com
 */
public class Login_Self {
	FormData formData = new FormData("-20");
	FormPanel loginpanel = new FormPanel();
	TextField<String> bcodecard = new TextField<String>();
	final protected LayoutContainer layoutcontainer = new LayoutContainer();

	public Login_Self() {
		init();
	}

	public void init() {

	}

	public LayoutContainer createForm() {

		layoutcontainer.setMonitorWindowResize(true);

		layoutcontainer.setStyleAttribute("background-color", "#ccd9e8");

		layoutcontainer.setHeight(Window.getClientHeight());
		layoutcontainer.setWidth(Window.getClientWidth());

		layoutcontainer.setLayout(new CenterLayout());

		// Image image = new Image(BaseResource.INSTANCE.assess_rule10());

		loginpanel.setHeading("登录");
		loginpanel.setFrame(true);
		loginpanel.setWidth(350);

		// loginpanel.add(image,formData);
		bcodecard.setFieldLabel("卡号");
		bcodecard.setName("name");
		bcodecard.setHeight(40);
		bcodecard.setAllowBlank(false);
		loginpanel.add(bcodecard, formData);

		Button b = new Button("确认");
		loginpanel.addButton(b);
		loginpanel.addButton(new Button("取消"));

		// loginpanel.addButton(newuser);

		loginpanel.setButtonAlign(HorizontalAlignment.CENTER);
		b.addSelectionListener(new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				// Window.alert("login...");
				login();

			}

		});

		KeyNav kn = new KeyNav(loginpanel) {
			public void onKeyPress(ComponentEvent ce) {
				if (ce.getKeyCode() == 13) {
					login();
				}
			}
		};
		layoutcontainer.add(loginpanel);
		return layoutcontainer;

	}

	void login() {
		if (!loginpanel.isValid()) {
			// Window.alert("loginpanel.isValid");
			return ;
		}
		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet("select bcode ,bname from bcode where exists " +
				"(select attendancecardno from bcode_job where" +
				" bcode_job.bcode=bcode.bcode and attendancecardno='"+bcodecard.getValue()+"')"
				, new AsyncCallback<DataSet>() {

			@Override
			public void onFailure(Throwable caught) {
				Window.alert("网络中断，请检查网络链接是否正常！");
			}

			@Override
			public void onSuccess(DataSet result) {

				if(result.getRowCount()==1)
				{
					Info.display("", "登录成功");
					Env.setEnvbcode(result.getValue(0, "bcode"));
					Env.setEnvname(result.getValue(0, "bname"));
				}
				else
				{
					MessageBox.alert("失败", "卡号录入错误", null);
					
				}
				
			}

		});

	}
}
